from manger import db
from marshmallow import Schema, fields, post_load

class CandidateStock(db.Model):

    __tablename__ = 'candidate_stocks'
    ID = db.Column(db.Integer, primary_key=True)
    Code = db.Column(db.String, nullable=False)
    CreateTime = db.Column(db.String, nullable=False)
    Date = db.Column(db.DateTime, nullable=False)
    Diff = db.Column(db.String, nullable=False)
    IsAv = db.Column(db.String, nullable=False)
    Message = db.Column(db.String, nullable=False)
    Name = db.Column(db.String, nullable=False)
    NowCost15 = db.Column(db.Float, nullable=False)
    NowCost30 = db.Column(db.Float, nullable=False)
    Score = db.Column(db.String, nullable=False)
    Status = db.Column(db.String, nullable=False)
    Step = db.Column(db.String, nullable=False)
    Topics = db.Column(db.String, nullable=False)
    TopicsHot = db.Column(db.String, nullable=False)
    UpdateTime = db.Column(db.Integer, nullable=False)
    kplTopics = db.Column(db.String, nullable=False)
    isSelected = db.Column(db.Integer, nullable=False, default=0)
    IsWin = db.Column(db.Integer, nullable=False)
    isCover15 = db.Column(db.Integer, nullable=False, default=0)
    isCover30 = db.Column(db.Integer, nullable=False, default=0)
    low = db.Column(db.Float, nullable=False)
    last_px = db.Column(db.Float, nullable=False)
    cover15Time = db.Column(db.DateTime, nullable=False)
    cover30Time = db.Column(db.DateTime, nullable=False)


class CandidateStockSchema(Schema):
    ID = fields.Integer()
    Code = fields.String()
    CreateTime = fields.Integer()
    Date = fields.DateTime()
    Diff = fields.Float()
    IsAv = fields.Integer()
    Message = fields.String()
    Name = fields.String()
    NowCost15 = fields.Float()
    NowCost30 = fields.Float()
    Score = fields.Float()
    Status = fields.Integer()
    Step = fields.Integer()
    Topics = fields.String()
    TopicsHot = fields.String()
    UpdateTime = fields.Integer()
    kplTopics = fields.String()
    isSelected = fields.Integer()
    IsWin = fields.Integer()
    isCover15 = fields.Integer()
    isCover30 = fields.Integer()
    low = fields.Float()
    last_px = fields.Float()
    cover15Time = fields.DateTime()
    cover30Time = fields.DateTime()

    @post_load  # 反序列化成一个对象，给schema的一个方法加上post_load装饰器
    def post_load(self, data, **kwargs):
        return CandidateStock(**data)

